package com.taoyuan.hotel.controller;

import com.alibaba.excel.EasyExcel;
import com.taoyuan.hotel.entity.Employee;
import com.taoyuan.hotel.entity.Hgoods;
import com.taoyuan.hotel.entity.Resident;
import com.taoyuan.hotel.service.intf.ResidentService;
import com.taoyuan.hotel.vo.PageBean;
import com.taoyuan.hotel.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/resident/")
public class ResidentController {
    @Autowired
    private ResidentService service;

    @PostMapping("save.do")
    public R save(Resident resident){
        return service.save(resident);
    }

    @GetMapping("page.do")
    public List<Resident> page(){
        return service.all() ;
    }

    @PostMapping("del.do")
    public R del(int id){
        return service.del(id);
    }

    @PostMapping("update.do")
    public R update(Resident resident){
        return service.update(resident);
    }

    @GetMapping("pageany.do")
    public PageBean pageAny(String name, int page, int limit){
        return service.selectAny(name, page, limit);
    }

    @PostMapping("delbatch.do")
    public R delBatch(@RequestBody int[] ids){
        return service.delBatch(ids);
    }

    @GetMapping("downall.do")
    public void down(HttpServletResponse response) throws IOException {
        //1.查询数据
        List<Resident> list = service.all();
        //2.设置响应消息头
        response.setHeader("Content-disposition", "attachment;filename=" +System.currentTimeMillis() + ".xlsx");
        //3.写出内容
        EasyExcel.write(response.getOutputStream(), Resident.class).
                sheet("自动导出").doWrite(list);
    }
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.saveBatch(file);
    }
}
